/*****************************************************************************************
  Project   : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
  Authors   : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
  Date      : August 2025
  Output    : Table 6 of Appendix
  Notes     : Requires estaddfirst to be run for to extract first stage
*****************************************************************************************/



clear
set more off
set type double,perm



// Notes
// Reflection specificaitons after Jan week 1 meeting
// requires estaddfirst to be run first
//
// Jan 15th.
// Sept 2023 - Revisions round 1, adding transitions

// data
use ${wd}/reg_dataRRv3_${ind}_${area}_all_case_0.dta , clear

// choose selection correction
local 3 "propic"

// Selection controls
if "`3'" == "propic" {
	foreach x of num 1/4{
		g dpropic_`x' = dpropic^`x'
		}
	global selectcnt dpropic_*
	global selecttest dpropic_1=dpropic_2=dpropic_3=dpropic_4=0
}

if "`3'" == "cert" {
	gen dterm1 = dcert1_c
	gen dterm2v1 = dcert2_c
	gen interact1 = dterm1 * dterm2

	foreach x of num 1/2{
		g dt1_`x' = dterm1^`x'
		g dt2_`x' = dterm2v1^`x'
		g in_`x' = 	interact1^`x'
		}
	global selectcnt dt1_* dt2_* in_*
	global selecttest dt1_1=dt1_2=dt2_1=dt2_2=in_1=in_2=0
}
		


// First stage reporting
if "$FSTAT" == "F" local FPOS = 8
if "$FSTAT" == "p" local FPOS = 11

// Table parameters

	// Outside options
	global rents  dE_c dE_cu  
	
	// IVs
	global IV1 IV_En IV_Eu
	


// clean up later
global inter c.LP#controls
global inter2 c.LP#(${area}#year)

	gen pdthreat = P_hat - LP	


drop if missing(dP)


// OLS
reghdfe dwage  $rents  dER  [ aw = weight ], a(controls  ${inter} ${inter2} ) vce(cl $clustlevel)
eststo c
estadd local rsq =  string(e(r2),"%3.2f") 
estadd local indic "Yes"
estadd local indic4 "Yes"
estadd local indic5 "Yes"

// IV
ivreghdfe dwage  dER     ($rents   =  $IV1 )  [ aw = weight ],a(controls  ${inter} ${inter2} )  cluster($clustlevel) first  
eststo d 

estadd scalar SW${FSTAT}n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}u 	= el(e(first),`FPOS', 2)

estadd local indic "Yes"
estadd local indic4 "Yes"
estadd local indic5 "Yes"
estadd local set " "
estadd local inst1 " \footnotesize{ $ \tilde{IV}_{nict} $ } "
estadd local inst2 " \footnotesize{ $ \tilde{IV}_{uict} $ } "


// IV
ivreghdfe dwage  dER dpropic_*    ($rents   =  $IV1  )  [ aw = weight ],a(controls  ${inter} ${inter2} )  cluster($clustlevel) first  
eststo e

estadd scalar SW${FSTAT}n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}u 	= el(e(first),`FPOS', 2)

estadd local indic "Yes"
estadd local indic4 "Yes"
estadd local indic5 "Yes"
estadd local pic "Yes"
estadd local set " "
estadd local inst1 " \footnotesize{ $ \tilde{IV}_{nict} $ } "
estadd local inst2 " \footnotesize{ $ \tilde{IV}_{uict} $ } "


// IV
ivreghdfe dwage  dpropic_*   ($rents = $IV1 )  [ aw = weight ],a(controls  ${inter} ${inter2} )  cluster($clustlevel) first  
eststo f

estadd scalar SW${FSTAT}n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}u 	= el(e(first),`FPOS', 2)

estadd local indic "Yes"
estadd local indic4 "Yes"
estadd local indic5 "Yes"
estadd local pic "Yes"
estadd local set " "
estadd local inst1 " \footnotesize{ $ \tilde{IV}_{nict} $ } "
estadd local inst2 " \footnotesize{ $ \tilde{IV}_{uict} $ } "

// IV
ivreghdfe dwage  dpropic_*   ($rents dP = $IV1 pdthreat )  [ aw = weight ],a(controls   )  cluster($clustlevel) first  
eststo g

estadd scalar SW${FSTAT}n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}p 	= el(e(first),`FPOS', 2)

estadd local indic "Yes"
estadd local indic4 " "
estadd local indic5 " "
estadd local pic "Yes"
estadd local set " "
estadd local inst1 " \footnotesize{ $ \tilde{IV}_{nict} $ } "
estadd local inst2 " \footnotesize{ $ \tilde{IV}_{uict} $ } "
estadd local inst3 " \footnotesize{ $ \Delta \hat{P}^{ne}_{ict}  $ } "

// IV
ivreghdfe dwage  dpropic_*   (dE_c dP = IV_En pdthreat )  [ aw = weight ],a(controls   )  cluster($clustlevel) first  
eststo h

estadd scalar SW${FSTAT}n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}p 	= el(e(first),`FPOS', 2)

estadd local indic "Yes"
estadd local indic4 " "
estadd local indic5 " "
estadd local pic "Yes"
estadd local set " "
estadd local inst1 " \footnotesize{ $ \tilde{IV}_{nict} $ } "
estadd local inst3 " \footnotesize{ $ \Delta \hat{P}^{ne}_{ict}  $ } "


esttab c d e  f g h , se r2 scalar(  ///
		"space2  \midrule First-Stage: " ///
		"SWF1  \ \ $ F $-Stat." "SWp1 \ \ \textit{p}-val" ///
		"SWF2  \ \ $ F $-Stat." "SWp2 \ \ \textit{p}-val" ///
		"OverID \ \ Over-id. \textit{p}-val")	
		
		
// Table notes
local notes This table displays results from the estimation of a linearized version of \eqref{wagefull} via OLS (column 1) and 2SLS (columns 2 - 5). ///
		 The dependent variable is the decadal change in the regression adjusted average hourly wage of non-union workers in an industry-city cell, ///
		 using CPS data from 1980-2010 across 50 industries and 93 cities. Standard errors, in parentheses, are clustered at the $clustlbl level.  
		
// Outside Option terms
lab var dE_c  "$ \Delta E_{nict} $"
lab var dE_1c "$ \Delta E_{nct|n i} $"
lab var dE_0c "$ \Delta E_{uct|n i} $"	
lab var dE_cu "$ \Delta E_{uict} $"				
lab var dP " $ \Delta P^{ne}_{ict} $ "

esttab   c d e  f g h using "${tables}//Table_Lin.tex", replace  ///
		booktabs nomtitles ///
		mgroups(OLS  2SLS, pattern(1  1 0 0 0 ) /// 
		prefix(\multicolumn{@span}{c}{) suffix(})  ///
		span erepeat(\cmidrule(lr){@span})) ///
		b(a2) se(a2) se  sfmt(3) star(* 0.10 ** 0.05 *** 0.01) label  ///
		keep(  $rents dE_c  dER dP  ) nonotes  tabnote(`notes')  ///
		title("Non-Union Wages and Outside Options: OLS and 2SLS Estimates `extlbl'  \label{tab:linearized:`1'`4'}") ///
		order( $rents dE_c  dER dP ) ///
		scalar( /// 
		"rsq $ R^2 $" ///
		"indic Year $\times$ Ind."  ///
		"indic4 $   P^{ne}_{ict-1}\times $ Ind. $ \times $ Year" ///
		"indic5 $   P^{ne}_{ict-1}\times $ City $ \times $ Year" ///
				"select \midrule Select controls" ///
		"pic \ \ \ $ \Delta P_{ic}$ Quadratic" ///
		"inst1 \midrule Instrument set:" ///
		"inst2 \ \ " ///  
		"inst3 \ \  " ///
		"space2  \midrule First-Stage $ ${FSTAT} $-Stat.: " ///
		"SW${FSTAT}n  \ \ \ \ $ \Delta E_{uict} $" ///
		"SW${FSTAT}u  \ \ \ \ $ \Delta E_{nict} $" ///
		"SW${FSTAT}p  \ \ \ \ $ \Delta P^{ne}_{ict} $")			

